package com.osdp.sc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.http.Result;
import com.osdp.common.util.BeansUtil;
import com.osdp.common.util.ConvertUtil;
import com.osdp.common.util.MessageUtil;
import com.osdp.sc.entity.ScRole;
import com.osdp.sc.service.IScRoleService;
import com.osdp.sc.vo.ScRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Api(tags = "角色信息")
@RestController
//@RefreshScope
@RequestMapping("/sc/role")
public class ScRoleController {

    @Autowired
    private IScRoleService roleService;

    /**
     *
     * @param roleVo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ScRoleVo>> pageRole(ScRoleVo roleVo,
                                          @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        ScRole scRole = ConvertUtil.sourceToTarget(roleVo, ScRole.class);
        IPage<ScRoleVo> result = roleService.pageList(scRole,new Page<>(pageNo, pageSize)).convert(track -> BeansUtil.copy(track, new ScRoleVo()));
        return Result.success(result);
    }

    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    public Result<List<ScRoleVo>> queryAllRole(
            @RequestParam(name = "roleName", required = false) String roleName,
            @RequestParam(name = "roleCode", required = false) String roleCode){
        List<ScRole> roleList = roleService.queryAllRole(roleName,roleCode);
        List<ScRoleVo> roles =  ConvertUtil.sourceToTarget( roleList, ScRoleVo.class);
        return Result.success(roles);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Result<ScRoleVo> getById(@RequestParam(name = "id", required = true) String id){
        ScRole role = roleService.getById(id);
        ScRoleVo roleVo = ConvertUtil.sourceToTarget( role, ScRoleVo.class);
        return Result.success(roleVo);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation("新增角色")
    public Result insert(@RequestBody JSONObject jsonObject) {
        ScRole role = JSON.parseObject(jsonObject.toJSONString(),ScRole.class);
        boolean isOk = roleService.insertRole(role);
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation("编辑角色")
    public Result update(@RequestBody JSONObject jsonObject) {
        ScRole role = roleService.getById(jsonObject.getString("id"));
        if (role == null) {
            return Result.fail(MessageUtil.getMessage(10054));
        }
        ScRole entity = JSON.parseObject(jsonObject.toJSONString(),ScRole.class);
        boolean isOk = roleService.updateRole(entity);
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除角色")
    public Result delete(@RequestParam(name = "id", required = true) String id) {
        if(StringUtils.isEmpty(id)) {
            return Result.fail(MessageUtil.getMessage(10051));
        }
        boolean isOk = roleService.deleteRole(id);
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    @ApiOperation("批量删除角色")
    public Result deleteBatcah(@RequestParam(name = "ids", required = true) String ids){
        if(StringUtils.isEmpty(ids)) {
            return Result.fail(MessageUtil.getMessage(10051));
        }
        boolean isOk = roleService.deleteBatcah(Arrays.asList(StringUtils.split(ids, ",")));
        return isOk ? Result.success(MessageUtil.getMessage(10049)) : Result.fail(MessageUtil.getMessage(10051));
    }

    /**
     * 根据用户id获取所有的角色
     * @param userId
     * @return
     */
    @GetMapping("/queryRolesByUserId")
    @ApiOperation("根据用户id获取所有的角色")
    public List<ScRoleVo> queryRolesByUserId(@RequestParam(name = "userId", required = true) String userId){
        return roleService.queryRolesByUserId(userId);
    }

    /**
     * 根据岗位id获取所有的角色
     * @param positionId 岗位ID
     * @return
     */
    @GetMapping("/queryRolesByPositionId")
    @ApiOperation("根据岗位id获取所有的角色")
    public List<ScRoleVo> queryRolesByPositionId(@RequestParam(name = "positionId", required = true) String positionId){
        return roleService.queryRolesByPositionId(positionId);
    }
}
